package org.smallreunion.tester;

import javax.servlet.http.Cookie;

import org.smallreunion.model.Member;
import org.smallreunion.service.AuthTokenService;
import org.smallreunion.service.MemberService;
import org.smallreunion.var.LoginInfo;

/**
 * ログイン状態を擬似的に作り出すヘルパー
 *
 * @author Sinmetal
 *
 */
public class LoginHelper {

    /** test auth token value */
    public final String AUTH_TOKEN_VALUE = "hoge";

    /** ログインビューヘルパー RequestScopeKey */
    public final String LOGIN_VIEW_HELPER = "loginViewHelper";

    /** Twitter内部ID */
    public final long TWITTER_ID = Long.MAX_VALUE;

    /** 名前(例：真) */
    public final String TWITTER_NAME = "真";

    /** ID(例：sinmetal) */
    public final String TWITTER_SCREEN_NAME = "sinmetal";

    /** Twitterプロフィール画像URL */
    public final String TWITTER_PROFILE_IMAGE_URL =
        "http://a2.twimg.com/profile_images/1042264867/1277990403846_bigger.png";

    private Member member;

    private MemberService memberService = new MemberService();

    private AuthTokenService authTokenService = new AuthTokenService();

    /**
     * テスト用のデフォルトメンバー情報を初期化
     */
    public LoginHelper() {
        member = new Member();
        member.setKey(Member.createKey(TWITTER_ID));
        member.setTwitterId(TWITTER_ID);
        member.setTwitterName(TWITTER_NAME);
        member.setTwitterScreenName(TWITTER_SCREEN_NAME);
        member.setTwitterProfileImageUrl(TWITTER_PROFILE_IMAGE_URL);
        memberService.insert(member);
        int minutes30 = 30 * 60;
        authTokenService.putLoginInfo(
            AUTH_TOKEN_VALUE,
            getLoginInfo(),
            minutes30);
    }

    /**
     * ログイン者Session情報取得
     *
     * @return
     */
    public LoginInfo getLoginInfo() {
        return LoginInfo.getInstance(member);
    }

    /**
     * ログイン者MemberModel取得
     *
     * @return
     */
    public Member getMember() {
        return member;
    }

    /**
     * get auth token cookie
     *
     * @return
     */
    public Cookie getAuthToken() {
        Cookie cookie = new Cookie("authToken", AUTH_TOKEN_VALUE);
        cookie.setPath("/");
        return cookie;
    }
}
